/**
 * Copyright (c) 2014, 2017, Oracle and/or its affiliates.
 * The Universal Permissive License (UPL), Version 1.0
 */
"use strict";
define(["ojs/ojcore","jquery","ojs/ojdatasource-common"],function(a){a.hb=function(g,b){if(!(g instanceof Array)&&"function"!=typeof g&&"function"!=typeof g.iOa)throw Error("_ERR_DATA_INVALID_TYPE_SUMMARY\n_ERR_DATA_INVALID_TYPE_DETAIL");this.lx=this.Lva(b);null!=b&&(this.columns=b.columns,this.sortCriteria=b.initialSort);a.hb.u.constructor.call(this,g)};o_("ArrayDataGridDataSource",a.hb,a);a.b.sa(a.hb,a.Rr,"oj.ArrayDataGridDataSource");a.hb.prototype.Init=function(){null==this.columns&&(this.columns=
this.Bua(this.em()));this.cza();"function"==typeof this.data&&this.data.subscribe(this.QEa.bind(this),null,"arrayChange");a.hb.u.Init.call(this)};a.b.g("ArrayDataGridDataSource.prototype.Init",{Init:a.hb.prototype.Init});a.hb.prototype.bm=null;a.b.g("ArrayDataGridDataSource.prototype.comparator",{bm:a.hb.prototype.bm});a.hb.prototype.sortCriteria=null;a.b.g("ArrayDataGridDataSource.prototype.sortCriteria",{sortCriteria:a.hb.prototype.sortCriteria});a.hb.prototype.Lva=function(a){if(null!=a&&null!=
a.rowHeader)if(a=a.rowHeader,"object"===typeof a){if(null!=a["default"]&&"none"==a["default"])return}else if(null!=a)return a;return"m_defaultIndex"};a.hb.prototype.cza=function(){var a;a=this.em();for(this.Sw=0;this.Sw<a.length;this.Sw+=1)a[this.Sw].ojKey=this.Sw.toString()};a.hb.prototype.Bua=function(a){var b,c;if("number"!==typeof a.length||0===a.length)return[];c=[];for(b in a[0])a[0].hasOwnProperty(b)&&(void 0!=this.lx&&b==this.lx||c.push(b));return c};a.hb.prototype.getCount=function(a){return"row"===
a?this.jt():"column"===a?this.columns.length:0};a.b.g("ArrayDataGridDataSource.prototype.getCount",{getCount:a.hb.prototype.getCount});a.hb.prototype.getCountPrecision=function(){return"exact"};a.b.g("ArrayDataGridDataSource.prototype.getCountPrecision",{getCountPrecision:a.hb.prototype.getCountPrecision});a.hb.prototype.vT=function(a,b){var c;if("row"===a){if(void 0==this.lx)return null;if("m_defaultIndex"==this.lx)return this.PJ(b);c=this.em();return c[b][this.lx]}if("column"===a)return this.columns[b]};
a.hb.prototype.ava=function(a,b){var c;if("row"===a)return{key:this.PJ(b)};if("column"===a)return c=this.vT(a,b),null!=this.sortCriteria&&this.sortCriteria.key===c?{key:this.vT(a,b),sortDirection:this.sortCriteria.direction}:{key:c}};a.hb.prototype.fetchHeaders=function(g,b,c){var d,e,f,h;d=g.axis;e=g.start;f=g.count;e=Math.max(0,e);"column"===d?f=Math.min(this.columns.length,e+f):(h=this.em(),f=void 0===this.lx?e:Math.min(h.length,e+f));d=new a.Eh(e,f,d,this);null!=b&&null!=b.success&&(null==c&&
(c={}),b.success.call(c.success,d,g,null))};a.b.g("ArrayDataGridDataSource.prototype.fetchHeaders",{fetchHeaders:a.hb.prototype.fetchHeaders});a.hb.prototype.rua=function(a,b){var c=this.columns[b];return this.em()[a][c]};a.hb.prototype.xDa=function(a,b,c){this.em()[a][this.columns[b]]=c};a.hb.prototype.sua=function(a,b){return{keys:{row:this.PJ(a),column:this.columns[b]}}};a.hb.prototype.fetchCells=function(g,b,c){var d,e,f,h,k,l;for(d=0;d<g.length;d+=1)e=g[d],"row"===e.axis?(f=e.start,h=Math.min(this.jt(),
f+e.count)):"column"===e.axis&&(k=e.start,l=Math.min(this.columns.length,k+e.count));void 0===h||void 0===l?null!=b&&null!=b.error&&(null==c&&(c={}),b.error.call(c.error)):(d=new a.wj(f,h,k,l,this),null!=b&&null!=b.success&&(null==c&&(c={}),b.success.call(c.success,d,g)))};a.b.g("ArrayDataGridDataSource.prototype.fetchCells",{fetchCells:a.hb.prototype.fetchCells});a.hb.prototype.keys=function(a){var b=a.row,c=a.column;return new Promise(function(a){a({row:this.PJ(b),column:this.columns[c]})}.bind(this))};
a.b.g("ArrayDataGridDataSource.prototype.keys",{keys:a.hb.prototype.keys});a.hb.prototype.indexes=function(a){var b=a.row,c=a.column;return new Promise(function(a){a({row:this.OJ(b),column:this.columns.indexOf(c)})}.bind(this))};a.b.g("ArrayDataGridDataSource.prototype.indexes",{indexes:a.hb.prototype.indexes});a.hb.prototype.sort=function(a,b,c){var d=[],e=[],f;null!=b&&null==c&&(c={});void 0==this.ex&&(this.aBa=this.sortCriteria,this.ex=this.data.slice());null==a?a=this.sortCriteria:this.sortCriteria=
a;if(null==a)this.tW(b,c);else if(f=a.axis,a=a.key,"column"===f)this.em().sort(this.gD()),null!=b&&null!=b.success&&b.success.call(c.success);else if("row"===f){a=this.OJ(a);for(f=0;f<this.columns.length;f+=1)d[f]=this.em()[a][this.columns[f]];d.sort(this.gD());for(f=0;f<this.columns.length;f+=1)e[f]=this.columns[d.indexOf(this.em()[a][this.columns[f]])];this.eja=this.columns;this.columns=e;null!=b&&null!=b.success&&b.success.call(c.success)}else null!==b&&null!=b.error&&b.error.call(c.error,"Invalid axis value")};
a.b.g("ArrayDataGridDataSource.prototype.sort",{sort:a.hb.prototype.sort});a.hb.prototype.tW=function(a,b){null!=this.ex&&(this.data=this.ex,this.sortCriteria=this.aBa);null!=this.eja&&(this.columns=this.eja);null!=a&&null!=a.success&&a.success.call(b.success)};a.hb.prototype.getCapability=function(a){return"sort"===a?"column":"move"===a?"row":null};a.b.g("ArrayDataGridDataSource.prototype.getCapability",{getCapability:a.hb.prototype.getCapability});a.hb.prototype.gD=function(){var a,b,c;a=this.comparator;
return null==a?(a=this.sortCriteria.key,b=this.sortCriteria.direction,c=this.sortCriteria.axis,this.FAa(b,a,c)):a};a.hb.prototype.FAa=function(a,b,c){if("ascending"===a)return function(a,e){var f,g;void 0!=b&&"column"==c&&(a instanceof Array?(a=a[parseInt(b,10)],e=e[parseInt(b,10)]):(a=a[b],e=e[b]));f=isNaN(a);g=isNaN(e);a instanceof Date&&(a=a.toISOString(),f=!0);e instanceof Date&&(e=e.toISOString(),g=!0);return f&&g?a<e?-1:a===e?0:1:f?1:g?-1:a-e};if("descending"===a)return function(a,e){var f,
g;void 0!=b&&"column"==c&&(a instanceof Array?(a=a[parseInt(b,10)],e=e[parseInt(b,10)]):(a=a[b],e=e[b]));f=isNaN(a);g=isNaN(e);a instanceof Date&&(a=a.toISOString(),f=!0);e instanceof Date&&(e=e.toISOString(),g=!0);return f&&g?a>e?-1:a===e?0:1:f?-1:g?1:e-a}};a.hb.prototype.move=function(a,b){var c,d;c=this.OJ(a);d=this.data.splice(c,1)[0];this.data instanceof Array&&(c=this.Io("delete",a,null,c,-1,!0),this.handleEvent("change",c));null===b?(this.data.push(d),c=this.data.length-1):(c=this.OJ(b),this.data.splice(c,
0,d));this.data instanceof Array&&(c=this.Io("insert",a,null,c,-1),this.handleEvent("change",c));null!=this.ex&&(this.ex=this.data.slice())};a.b.g("ArrayDataGridDataSource.prototype.move",{move:a.hb.prototype.move});a.hb.prototype.moveOK=function(){return"valid"};a.b.g("ArrayDataGridDataSource.prototype.moveOK",{moveOK:a.hb.prototype.moveOK});a.hb.prototype.em=function(){return"function"===typeof this.data?this.data():this.data};a.hb.prototype.OJ=function(a){var b,c=this.em();for(b=0;b<c.length;b++)if(c[b].ojKey===
a)return b;return-1};a.hb.prototype.PJ=function(a){var b=this.em();return b[a]?b[a].ojKey:null};a.hb.prototype.Io=function(a,b,c,d,e,f){var h={source:this};h.operation=a;h.keys={row:b,column:c};h.indexes={row:d,column:e};h.silent=f;return h};a.hb.prototype.QEa=function(a){var b,c,d,e=!1;c=!1;var f=[],h=[];for(b=0;b<a.length;b++){d=a[b];if(void 0!==d.moved){c=!0;d=this.Io("refresh",null,null);this.handleEvent("change",d);break}"added"===d.status&&(e=!0)}if(!c){for(b=0;b<a.length;b++)d=a[b],"deleted"===
d.status&&(c=d.value,d=d.index,c=c.ojKey,f.push({row:c,column:-1}),h.push({row:d,column:-1}));0<f.length&&(d={source:this,operation:"delete",keys:f,indexes:h,silent:e},this.handleEvent("change",d));for(b=0;b<a.length;b++)d=a[b],"added"===d.status&&(c=d.value,d=d.index,null==c.ojKey&&(c.ojKey=this.Sw.toString(),this.Sw++),c=c.ojKey,d=this.Io("insert",c,null,d,-1),this.handleEvent("change",d))}null!=this.ex&&(this.ex=this.data.slice())};a.hb.prototype.jt=function(){return this.em().length};a.hb.prototype.Lga=
function(){return this.lx};a.b.g("ArrayDataGridDataSource.prototype.getRowHeaderKey",{Lga:a.hb.prototype.Lga});a.hb.prototype.Bt=function(){return this.columns};a.b.g("ArrayDataGridDataSource.prototype.getColumns",{Bt:a.hb.prototype.Bt});a.hb.prototype.getData=function(){return this.data};a.b.g("ArrayDataGridDataSource.prototype.getData",{getData:a.hb.prototype.getData});a.Eh=function(a,b,c,d){this.fd=a;this.pg=b;this.gia=c;this.jm=d};o_("ArrayHeaderSet",a.Eh,a);a.Eh.prototype.getData=function(g,
b){if(null==this.jm)return null;a.p.assert(g<=this.pg&&g>=this.fd,"index out of bounds");a.p.assert(null==b||0==b,"level out of bounds");return this.jm.vT(this.gia,g)};a.b.g("ArrayHeaderSet.prototype.getData",{getData:a.Eh.prototype.getData});a.Eh.prototype.getMetadata=function(g,b){if(null==this.jm)return null;a.p.assert(g<=this.pg&&g>=this.fd,"index out of bounds");a.p.assert(null==b||0==b,"level out of bounds");return this.jm.ava(this.gia,g)};a.b.g("ArrayHeaderSet.prototype.getMetadata",{getMetadata:a.Eh.prototype.getMetadata});
a.Eh.prototype.getLevelCount=function(){return 0<this.getCount()?1:0};a.b.g("ArrayHeaderSet.prototype.getLevelCount",{getLevelCount:a.Eh.prototype.getLevelCount});a.Eh.prototype.getExtent=function(g,b){a.p.assert(g<=this.pg&&g>=this.fd,"index out of bounds");a.p.assert(null==b||0==b,"level out of bounds");return{extent:1,more:{before:!1,after:!1}}};a.b.g("ArrayHeaderSet.prototype.getExtent",{getExtent:a.Eh.prototype.getExtent});a.Eh.prototype.getDepth=function(g,b){a.p.assert(g<=this.pg&&g>=this.fd,
"index out of bounds");a.p.assert(null==b||0==b,"level out of bounds");return 1};a.b.g("ArrayHeaderSet.prototype.getDepth",{getDepth:a.Eh.prototype.getDepth});a.Eh.prototype.getCount=function(){return null==this.jm?0:Math.max(0,this.pg-this.fd)};a.b.g("ArrayHeaderSet.prototype.getCount",{getCount:a.Eh.prototype.getCount});a.Eh.prototype.getStart=function(){return this.fd};a.b.g("ArrayHeaderSet.prototype.getStart",{getStart:a.Eh.prototype.getStart});a.wj=function(a,b,c,d,e){this.Ea=a;this.ob=b;this.Mp=
c;this.Uw=d;this.jm=e};o_("ArrayCellSet",a.wj,a);a.wj.prototype.getData=function(a){var b,c,d;b=this;c=a.row;d=a.column;a={};Object.defineProperty(a,"data",{get:function(){return b.jm.rua(c,d)},set:function(a){b.jm.xDa(c,d,a)}});return a};a.b.g("ArrayCellSet.prototype.getData",{getData:a.wj.prototype.getData});a.wj.prototype.getMetadata=function(a){return this.jm.sua(a.row,a.column)};a.b.g("ArrayCellSet.prototype.getMetadata",{getMetadata:a.wj.prototype.getMetadata});a.wj.prototype.getStart=function(a){return"row"==
a?this.Ea:"column"==a?this.Mp:-1};a.b.g("ArrayCellSet.prototype.getStart",{getStart:a.wj.prototype.getStart});a.wj.prototype.getCount=function(a){return"row"===a?Math.max(0,this.ob-this.Ea):"column"===a?Math.max(0,this.Uw-this.Mp):0};a.b.g("ArrayCellSet.prototype.getCount",{getCount:a.wj.prototype.getCount});a.wj.prototype.Ap=function(){return this.Ea};a.b.g("ArrayCellSet.prototype.getStartRow",{Ap:a.wj.prototype.Ap});a.wj.prototype.zp=function(){return this.Mp};a.b.g("ArrayCellSet.prototype.getStartColumn",
{zp:a.wj.prototype.zp})});